package com.wontube.smr.dao.bo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.dao.mapper.GenreMapper;
import com.wontube.smr.dao.mapper.web.GenreSiteMapper;
import com.wontube.smr.pub.DefaultDBHelper;
import com.wontube.smr.web.client.model.po.GenreSite;


/**
 * @author yuechao
 *
 */
public final class GenreSelectDao {
	private GenreSelectDao() {
	}
	
	public static List<Genre> selectGenresByCategory(String category) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			GenreMapper mapper = session.getMapper(GenreMapper.class);
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("category = ", category);
			ex.setOrderByClause("genre_name asc");
			return mapper.selectByExample(ex);
		} finally {
			session.close();
		}
	}
	
	public static List<Genre> selectGenresWithoutOthers(String category) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			GenreMapper mapper = session.getMapper(GenreMapper.class);
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("category = ", category)
				.addCriterion("display_name !='Others'");
			ex.setOrderByClause("genre_name asc");
			return mapper.selectByExample(ex);
		} finally {
			session.close();
		}
	}
	
	public static List<Genre> selectGenres(String category){
		return selectGenresByCategory(category);
	}
	
	/**
	 * 筛选分类中一对多的genres
	 * @param category
	 * @return
	 */
	public static Map<String, List<String>> selectMultiGenres(String category){
		Map<String, List<String>> genreMap = new HashMap<String, List<String>>();
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try{
			GenreMapper mapper = session.getMapper(GenreMapper.class);
			List<String> displayNames = mapper.listMultiDisplayGenres(category);
			for (String name : displayNames) {
				List<String> genreNames = mapper.listMultiGenreNames(category, name);
				genreMap.put(name, genreNames);
			}
		} finally {
			session.close();
		}		
		
		return genreMap;
	}
	
	
	public static List<Genre> selectAllGenres(){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try{
			GenreMapper mapper = session.getMapper(GenreMapper.class);
			QueryExample ex = new QueryExample();
			ex.setOrderByClause("version desc");
			return mapper.selectByExample(ex);
		} finally {
			session.close();
		}
	}
	
	public static boolean saveOrUpdateGenre(Genre genre){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			GenreMapper mapper = session.getMapper(GenreMapper.class);
			Genre temp = mapper.selectByPrimaryKey(genre.getId());
			int aff = 0;
			if(temp != null){
				aff = mapper.updateByPrimaryKey(genre);
			}else{
				aff = mapper.insert(genre);
			}
			return aff == 1;
		} finally {
			session.close();
		}
	}
	
	public static boolean addGenre(Genre genre){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			GenreMapper mapper = session.getMapper(GenreMapper.class);
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("genre.genre_name=", genre.getGenreName())
					.addCriterion("genre.category=", genre.getCategory());
			int count = mapper.countByExample(ex);
			if(count > 0){
				return false;
			}
		
			mapper.insert(genre);
			return true;
		} finally {
			session.close();
		}
	}
	
	public static boolean deleteGenre(String pk){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			GenreMapper mapper = session.getMapper(GenreMapper.class);
			int aff = mapper.deleteByPrimaryKey(pk);
			return aff == 1;
		} catch (Exception e) {
			return false;
		}finally{
			session.close();
		}
	}
	
	public static final boolean updateGenreByPK(Genre genre){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try{
			GenreMapper mapper = session.getMapper(GenreMapper.class);
			int aff = mapper.updateByPrimaryKey(genre);
			return aff == 1;
		}finally{
			session.close();
		}
	}
	public static final Genre getGenreByPK(String pk){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try{
			GenreMapper mapper = session.getMapper(GenreMapper.class);
			Genre genre = mapper.selectByPrimaryKey(pk);
			return genre;
		}finally{
			session.close();
		}
	}
	
	public static List<String> listGenreCategorys(){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try{
			GenreMapper mapper = session.getMapper(GenreMapper.class);
			return mapper.listGenreCategories();
		} finally {
			session.close();
		}
	}
	
	public static List<GenreSite> selectGenreSites(String website, String category){
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try{
			GenreSiteMapper mapper = session.getMapper(GenreSiteMapper.class);
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("genre_site.genre_category=", category)
					.addCriterion("genre_site.website=", website);
			return mapper.selectGenreSiteWith(ex);
		} finally {
			session.close();
		}		
	}
}
